package com.wei.web.middleware.httpfilter.registrar.filter;

import com.wei.web.middleware.httpfilter.registrar.IRegistrar;
import com.wei.web.middleware.httpfilter.registrar.filter.factory.BeanFilterRegistrarFactoryBean;
import com.wei.web.middleware.httpfilter.registrar.filter.factory.FactoryFilterRegistrarFactoryBean;
import com.wei.web.middleware.httpfilter.registrar.filter.factory.ObjectFilterRegistrarFactoryBean;
import com.wei.web.middleware.httpfilter.registrar.model.FilterConfigDTO;
import com.wei.web.middleware.httpfilter.registrar.model.RegisterContext;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.util.StringUtils;
import org.springframework.web.filter.GenericFilterBean;

import java.util.List;

public abstract class AbstractFilterRegistrar implements IRegistrar {

    protected String getFilterNameWithDefault(FilterConfigDTO filterConfigDTO, RegisterContext registerContext) {
        if (StringUtils.isEmpty(filterConfigDTO.getName())) {
            String defaultName = getType() + registerContext.getFilterOrder();
            return defaultName;
        }
        return filterConfigDTO.getName();
    }

    protected void doRegisterWithObject(FilterConfigDTO filterConfig,
                                        String filterName,
                                        Integer order,
                                        List<String> urlPatterns,
                                        GenericFilterBean filter,
                                        BeanDefinitionRegistry registry
                                    ) {

        BeanDefinitionBuilder builder1 = BeanDefinitionBuilder.genericBeanDefinition(ObjectFilterRegistrarFactoryBean.class);
        builder1.addPropertyValue("filterConfig", filterConfig);
        builder1.addPropertyValue("filterName", filterName);
        builder1.addPropertyValue("filterOrder", order);
        builder1.addPropertyValue("urlPatterns", urlPatterns);
        builder1.addPropertyValue("filter", filter);
        String beanName = filterName + ObjectFilterRegistrarFactoryBean.class.getSimpleName();
        registry.registerBeanDefinition(beanName, builder1.getBeanDefinition());
    }

    protected void doRegisterWithBeanName(FilterConfigDTO filterConfig,
                                        String filterName,
                                        Integer order,
                                        List<String> urlPatterns,
                                        String filterBeanName,
                                        BeanDefinitionRegistry registry
    ) {

        BeanDefinitionBuilder builder1 = BeanDefinitionBuilder.genericBeanDefinition(BeanFilterRegistrarFactoryBean.class);
        builder1.addPropertyValue("filterConfig", filterConfig);
        builder1.addPropertyValue("filterName", filterName);
        builder1.addPropertyValue("filterOrder", order);
        builder1.addPropertyValue("urlPatterns", urlPatterns);
        builder1.addPropertyReference("filter", filterBeanName);
        String beanName = filterName + BeanFilterRegistrarFactoryBean.class.getSimpleName();
        registry.registerBeanDefinition(beanName, builder1.getBeanDefinition());
    }

    protected void doRegisterWithFactoryBeanName(FilterConfigDTO filterConfig,
                                        String filterName,
                                        Integer order,
                                        List<String> urlPatterns,
                                                 String filterFactoryBeanName,
                                        BeanDefinitionRegistry registry
    ) {

        BeanDefinitionBuilder builder1 = BeanDefinitionBuilder.genericBeanDefinition(FactoryFilterRegistrarFactoryBean.class);
        builder1.addPropertyValue("filterConfig", filterConfig);
        builder1.addPropertyValue("filterName", filterName);
        builder1.addPropertyValue("filterOrder", order);
        builder1.addPropertyValue("urlPatterns", urlPatterns);
        builder1.addPropertyReference("filterFactory", filterFactoryBeanName);
        String beanName = filterName + FactoryFilterRegistrarFactoryBean.class.getSimpleName();
        registry.registerBeanDefinition(beanName, builder1.getBeanDefinition());
    }
}
